package com.example.zrq1010110087.graceplayer.main;

import android.app.Activity;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zrq1010110087.graceplayer.R;
import com.example.zrq1010110087.graceplayer.data.MediaPlayerUser;
import com.example.zrq1010110087.graceplayer.data.Music;
import com.example.zrq1010110087.graceplayer.data.MusicList;

import java.util.ArrayList;

public class MainActivity extends Activity {
   //设置全局引用
    private ImageButton imgBtn_Previous;
    private ImageButton imgBtn_Play;
    private ImageButton imgBtn_Stop;
    private ImageButton imgBtn_Next;
    private ListView list;
    private ArrayList<Music> musicArrayList;
    private MediaPlayerUser mediaPlayerUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewByI();
        registerListener();
        initMusicList();
        initListView();
        checkMusicFile();
        initMediaPlayerUser();

    }
    //绑定对象
    private void findViewByI(){

         imgBtn_Previous= (ImageButton) findViewById(R.id.button_previous);
        imgBtn_Play= (ImageButton) findViewById(R.id.button_play);
        imgBtn_Stop= (ImageButton) findViewById(R.id.button_stop);
        imgBtn_Next= (ImageButton) findViewById(R.id.button_next);
        list=(ListView)findViewById(R.id.listview);

    }
    //获得手机内存的音乐信息
     private void initMusicList(){

        musicArrayList= MusicList.getMusicArrayList();
        if(musicArrayList.isEmpty()){
            //调用
            Cursor cursor=getContentResolver().query(Media.EXTERNAL_CONTENT_URI,null,null,null,Media.TITLE);
            if(cursor!=null) {
                int muscicTitle = cursor.getColumnIndex("title");
                int muscicArtist = cursor.getColumnIndex("artist");
                int muscicTotalTime = cursor.getColumnIndex("duration");
                int muscicPath = cursor.getColumnIndex("_data");
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {

                    String title = cursor.getString(muscicTitle);
                    String artist = cursor.getString(muscicArtist);
                    String totalTime = cursor.getString(muscicTotalTime);
                    String path = cursor.getString(muscicPath);
                    Music music=new Music(title,artist,path,totalTime);
                    musicArrayList.add(music);
                    cursor.moveToNext();
                }

            }



        }

    }
    //初始化MediaPlayerUser
    public void initMediaPlayerUser(){

        if(!musicArrayList.isEmpty())
        mediaPlayerUser=new MediaPlayerUser(musicArrayList,this);

    }

    //点击事件绑定
    private void registerListener(){

        imgBtn_Previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mediaPlayerUser.moveToPrevious(MainActivity.this);

            }
        });
        imgBtn_Play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayerUser.Playing()){

                    mediaPlayerUser.pause();
                    imgBtn_Play.setBackgroundResource(R.drawable.button_play);

                }else {
                    mediaPlayerUser.play();
                    imgBtn_Play.setBackgroundResource(R.drawable.button_pause);
                }
            }
        });
        imgBtn_Stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mediaPlayerUser.stop();


            }
        });
        imgBtn_Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mediaPlayerUser.moveToNext(MainActivity.this);

            }
        });
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               if(!musicArrayList.isEmpty()){

               mediaPlayerUser.setMusicNumber(i);
               mediaPlayerUser.play();

               }
           }
       });

    }
    //检查SD卡是否存在音乐,限制用户操作
    private void checkMusicFile(){

           if(musicArrayList.isEmpty()){

               imgBtn_Next.setEnabled(false);
               imgBtn_Play.setEnabled(false);
               imgBtn_Previous.setEnabled(false);
               imgBtn_Stop.setEnabled(false);
               Toast.makeText(MainActivity.this,this.getString(R.string.musiclist_null),Toast.LENGTH_SHORT).show();

           }
           else{


               imgBtn_Next.setEnabled(true);
               imgBtn_Play.setEnabled(true);
               imgBtn_Previous.setEnabled(true);
               imgBtn_Stop.setEnabled(true);

           }


    }
   //将获得的音乐信息根据需要以列表元素信息显示
    private  void initListView(){

         list.setAdapter(new MyAdapter());

    }
    //自定义适 配器
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return musicArrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            if(view==null){

                v=View.inflate(MainActivity.this,R.layout.listview_items,null);
            }else{

                v=view;
            }
            TextView tv_number=(TextView)v.findViewById(R.id.tv_listview_item_number);
            TextView tv_title=(TextView)v.findViewById(R.id.tv_listview_item_title);
            TextView tv_artist=(TextView)v.findViewById(R.id.tv_listview_item_artist);
            tv_artist.setText(musicArrayList.get(i).getmusicArtist());
            tv_title.setText(musicArrayList.get(i).getmusicName());
            tv_number.setText(i+"");

            return v;
        }
    }
}
